//
//  SCNInvestDetailViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/11/21.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNInvestDetailViewController: SCNTableViewController {

    var collectionView: UICollectionView!
    
    let itemIdentifier1 = "SCNInvestDetailCell1"
    let itemIdentifier2 = "SCNInvestDetailCell2"
    
    var loansn: String?
    var dataresult: SCNInvestDetailDataresultModel?
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let tmpNavImg = UIImage().getImageWithColor(CGSize(width: SCNConstants.SCREENWIDTH, height: SCNConstants.NavigationHeight), color: SCNConstants.rootBackgroundColor)
        self.navigationController?.navigationBar.setBackgroundImage(tmpNavImg, for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        configurationUI()
        
        request()
    }


    // MARK: UI
    
    func configurationUI() {
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: view.frame.size.width, height: view.frame.size.height - SCNConstants.NavigationHeight)
        layout.minimumLineSpacing = 0
        
        collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height - SCNConstants.NavigationHeight), collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.isPagingEnabled = true
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.bounces = false
        collectionView.backgroundColor = .white
        view.addSubview(collectionView)
        
        collectionView.register(SCNInvestDetailCell1.self, forCellWithReuseIdentifier: itemIdentifier1)
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: itemIdentifier2)
    }
    
    // MARK: DATA
    override func request() {
        
        super.request()

        if let tmploansn = loansn {
            
            let param = ["loansn" : tmploansn]
            
            SCNInvestDetailViewModel().requestInvestDetailInvestDetailNew(param, successBlock: { result in
                self.dataresult = result
                self.collectionView.reloadData()
            })
            
        }
        
        
    }
}

extension SCNInvestDetailViewController: UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        switch indexPath.item {
        case 0:
            let item1 = collectionView.dequeueReusableCell(withReuseIdentifier: itemIdentifier1, for: indexPath) as! SCNInvestDetailCell1
            if let tmpResult = dataresult {
                item1.setSCNInvestDetailCell1(tmpResult, index: indexPath)
            }
            return item1
        case 1:
            let item2 = collectionView.dequeueReusableCell(withReuseIdentifier: itemIdentifier2, for: indexPath)
            return item2
        default:
            return UICollectionViewCell()
        }
        
    }
}
